package com.quanquan.management.controller;

import DTO.PageDTO;
import com.quanquan.management.dto.KetopParkingRecordDTO;
import com.quanquan.management.request.ParkingRecordRequest;
import com.quanquan.management.service.KetopParkingService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import web.response.ResultBody;

/**
 * @ClassName:KetopParkingController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/9/26 14:37
 * @Version 1.0
 */
@RequestMapping("/ketop_parking")
@Controller
public class KetopParkingController extends ManagerBaseController {

    private final KetopParkingService ketopParkingService;

    @Autowired
    public KetopParkingController(KetopParkingService ketopParkingService) {
        this.ketopParkingService = ketopParkingService;
    }

    /**
     * 停车缴费记录
     *
     * @param pageSize
     * @param page
     * @param keyword
     * @param sort
     * @param fromDate
     * @param toDate
     * @param isAsc
     * @param model
     * @return
     */
    @GetMapping("/parking_record")
    public String parkingRecord(@RequestParam(required = false) Integer pageSize,
                                @RequestParam(required = false) Integer page,
                                @RequestParam(required = false) String keyword,
                                @RequestParam(required = false) String sort,
                                @RequestParam(required = false) String fromDate,
                                @RequestParam(required = false) String toDate,
                                @RequestParam(required = false) boolean isAsc,
                                Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        ParkingRecordRequest requestBody = new ParkingRecordRequest(page, pageSize, keyword, sort, isAsc);
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        PageDTO<KetopParkingRecordDTO> pageDTO = ketopParkingService.parkingRecord(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "ketopParking/parking_record";
    }

    /**
     * 导出停车缴费记录
     *
     * @param keyword
     * @param fromDate
     * @param toDate
     * @param total
     * @return
     */
    @RequestMapping("/export_parking_record")
    public ResultBody exportParkingRecord(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String fromDate,
            @RequestParam(required = false) String toDate,
            @RequestParam(required = false) Integer total
    ) {
        ParkingRecordRequest requestBody = new ParkingRecordRequest();
        requestBody.setFromDate(fromDate);
        requestBody.setToDate(toDate);
        requestBody.setKeyword(keyword);
        requestBody.setSize(total);
        try{
            ketopParkingService.exportParkingRecord(this.response,requestBody);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
    }


}
